import os
import shutil
from pathlib import Path
from collections import defaultdict

class FileOrganizer:
    def __init__(self, source_dir):
        self.source_dir = Path(source_dir)
        self.file_types = defaultdict(list)

    def analyze_files(self):
        """统计文件类型"""
        for file in self.source_dir.glob("*"):
            if file.is_file():
                ext = file.suffix[1:].lower() if file.suffix else "no_extension"
                self.file_types[ext].append(file.name)

    def organize_files(self, target_dir="Organized"):
        """按类型整理文件"""
        target = self.source_dir / target_dir
        target.mkdir(exist_ok=True)
        
        for ext, files in self.file_types.items():
            ext_dir = target / ext
            ext_dir.mkdir(exist_ok=True)
            for file in files:
                shutil.move(str(self.source_dir / file), str(ext_dir))